document.getElementById("form").addEventListener("submit", function (e) {
  e.preventDefault();
  const formData = new FormData(this);
  const obj = {};
  formData.forEach((v, k) => obj[k] = v);
  obj.timestamp = new Date().toISOString().split("T")[0];
  let entries = JSON.parse(localStorage.getItem("entries") || "[]");
  entries.push(obj);
  localStorage.setItem("entries", JSON.stringify(entries));
  renderTable(entries);
});

function renderTable(data) {
  const table = document.getElementById("outputTable");
  table.innerHTML = "<tr>" + Object.keys(data[0]).map(k => `<th>${k}</th>`).join("") + "</tr>" +
    data.map(row => "<tr>" + Object.values(row).map(val => `<td>${val}</td>`).join("") + "</tr>").join("");
}

window.onload = () => {
  const data = JSON.parse(localStorage.getItem("entries") || "[]");
  if (data.length > 0) renderTable(data);
};
